﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Jacaranda.Data;

namespace Jacaranda.Model
{
    public class IsDistinctFunction : UnaryFunction
    {
        internal override string GenerateSql(Type propertyEntityType)
        {
            string keyFieldName = Metadata.Type(propertyEntityType).PrimaryKey.FieldName;

            return string.Format("{0} IN (SELECT MAX ({1}) FROM {2} GROUP BY {3})",
                keyFieldName,
                keyFieldName,
                Metadata.Type(propertyEntityType).TableName,
                Operand.GenerateSql(propertyEntityType));
            
        }
    }
}
